package cn.itcast.consumer.web;

import cn.itcast.consumer.config.RedisProperties;
import cn.itcast.consumer.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * @author ITHEIMA
 */
@RestController
@RequestMapping("consumer")
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private RedisProperties redisProperties;

    @Autowired
    DiscoveryClient discoveryClient;

    @GetMapping("/rest/{id}")
    public User consumerUserById(@PathVariable("id") String id){
//        String url = "http://localhost:8081/user/" + id;

//        List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
//        ServiceInstance instance = instances.get(0);
//        String uri = instance.getUri().toString();
//        String url = uri+ "/user/" + id;

        String url = "http://user-service/user/" + id;
        return restTemplate.getForObject(url, User.class);
    }

    @GetMapping("/hello")
    public RedisProperties disUserById(){
        return redisProperties;
    }



    @GetMapping("nacos/list")
    public Map findAll(Integer page, Integer size){
        String url = "http://192.168.200.150:8848/nacos/v1/ns/service/list?pageNo="+page+"&pageSize="+size;
        return restTemplate.getForObject(url, Map.class);
    }
}
